import tkinter as tk
from tkinter import messagebox
import threading
import time
import random
import winsound

class ReminderApp:
    def __init__(self, root):
        self.root = root
        self.root.title('定时提醒器')
        self.running = False

        # 输入区
        input_frame = tk.Frame(root)
        input_frame.pack(padx=20, pady=15)
        tk.Label(input_frame, text='最小间隔a（分钟）:').grid(row=0, column=0, sticky='e', pady=2)
        tk.Label(input_frame, text='最大间隔b（分钟）:').grid(row=1, column=0, sticky='e', pady=2)
        tk.Label(input_frame, text='总时长m（分钟）:').grid(row=2, column=0, sticky='e', pady=2)

        self.a_var = tk.StringVar(value='3')
        self.b_var = tk.StringVar(value='5')
        self.m_var = tk.StringVar(value='90')

        tk.Entry(input_frame, textvariable=self.a_var, width=10).grid(row=0, column=1, pady=2)
        tk.Entry(input_frame, textvariable=self.b_var, width=10).grid(row=1, column=1, pady=2)
        tk.Entry(input_frame, textvariable=self.m_var, width=10).grid(row=2, column=1, pady=2)

        # 按钮区
        btn_frame = tk.Frame(root)
        btn_frame.pack(pady=5)
        self.start_btn = tk.Button(btn_frame, text='开始', width=10, command=self.start)
        self.start_btn.grid(row=0, column=0, padx=5)
        self.pause_btn = tk.Button(btn_frame, text='暂停', width=10, command=self.toggle_pause, state='disabled')
        self.pause_btn.grid(row=0, column=1, padx=5)

        # 状态区
        status_frame = tk.Frame(root)
        status_frame.pack(pady=10)
        self.status_label = tk.Label(status_frame, text='', font=('微软雅黑', 11))
        self.status_label.pack(pady=2)
        self.time_label = tk.Label(status_frame, text='已用总时间：0 秒', font=('微软雅黑', 11))
        self.time_label.pack(pady=2)

        self.paused = False
        self.elapsed_seconds = 0

    def beep(self):
        winsound.Beep(852, 1500)

    def remind(self):
        messagebox.showinfo('提醒', '时间到！')
        self.beep()

    def update_time_label(self):
        if self.running:
            self.time_label.config(text=f'已用总时间：{self.elapsed_seconds} 秒')
            self.root.after(1000, self.update_time_label)

    def toggle_pause(self):
        if not self.running:
            return
        self.paused = not self.paused
        if self.paused:
            self.pause_btn.config(text='继续')
        else:
            self.pause_btn.config(text='暂停')

    def run_timer(self, a, b, m):
        start_time = time.time()
        end_time = start_time + m * 60
        self.elapsed_seconds = 0
        self.root.after(1000, self.update_time_label)
        while self.running and time.time() < end_time:
            now = time.time()
            interval = random.uniform(a, b)
            next_time = now + interval * 60
            left = max(0, int(next_time - time.time()))
            for i in range(left):
                if not self.running:
                    return
                while self.paused:
                    time.sleep(0.2)
                    if not self.running:
                        return
                self.status_label.config(text=f'下次提醒剩余：{left - i} 秒')
                time.sleep(1)
                self.elapsed_seconds += 1
            if not self.running:
                return
            self.root.after(0, self.remind)
            for _ in range(10):
                if not self.running:
                    return
                while self.paused:
                    time.sleep(0.2)
                    if not self.running:
                        return
                time.sleep(1)
                self.elapsed_seconds += 1
            if not self.running:
                return
            self.root.after(0, self.remind)
        self.status_label.config(text='提醒结束')
        self.running = False
        self.pause_btn.config(state='disabled')

    def start(self):
        if self.running:
            return
        try:
            a = float(self.a_var.get())
            b = float(self.b_var.get())
            m = float(self.m_var.get())
            if a <= 0 or b <= 0 or m <= 0 or a > b:
                raise ValueError
        except ValueError:
            messagebox.showerror('输入错误', '请输入有效的数字，且a<=b且都大于0')
            return
        self.running = True
        self.paused = False
        self.pause_btn.config(text='暂停', state='normal')
        self.status_label.config(text='计时中...')
        self.beep()  # 开始时响一声
        threading.Thread(target=self.run_timer, args=(a, b, m), daemon=True).start()

if __name__ == '__main__':
    root = tk.Tk()
    app = ReminderApp(root)
    root.mainloop() 